///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2009. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

package
{

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.ProgressEvent;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

/**
 *  @author				Dimarik
 *  @version			1.0
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="1024", height="768", frameRate="21", backgroundColor="#E0EEEE")]

public class LoaderTest extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Class methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function LoaderTest() {
		super();
		super.addEventListener(Event.ADDED_TO_STAGE, this.initialize);
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------
	
	private var _loader:		Loader;

	private var _root:			DisplayObject;

	private var _bitmapData:	BitmapData;

	private var _appDomain:		ApplicationDomain;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function initialize(event:Event):void {
		(event.target as IEventDispatcher).removeEventListener(event.type, arguments.callee);

		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var url:		String = 'http://www.pixeljoint.com/files/icons/full/tree2__r1368268481.gif';
		//var url:		String = 'http://onflex.org/flexapps/applications/ProgressiveImageLoading/jpg.jpg';
		var request:	URLRequest = new URLRequest(url);
		var loader:		Loader = new Loader();
		var context:	LoaderContext = new LoaderContext();
		//context.applicationDomain = this._appDomain = new ApplicationDomain(ApplicationDomain.currentDomain);

		loader.load(request);

		var li:			LoaderInfo = loader.contentLoaderInfo;
		li.addEventListener(Event.COMPLETE,					this.handler_complete);
		//li.addEventListener(ProgressEvent.PROGRESS,			this.handler_progress);
		//li.addEventListener(HTTPStatusEvent.HTTP_STATUS,	this.handler_httpStatus);
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private function handler_complete(event:Event):void {
		var li:			LoaderInfo = event.target as LoaderInfo;
		var loader:		Loader = li.loader;
		//li.removeEventListener(Event.COMPLETE,					this.handler_complete);

		var bitmap:Bitmap = li.content as Bitmap;
		trace(bitmap.parent)

		var sprite:Sprite = new Sprite();
		sprite.addChild(bitmap);
		trace(bitmap.parent)
		li.loader.unloadAndStop();

		var bitmapData:		BitmapData;

		switch (2) { // root bitmapData
			case 1: 
				bitmapData = bitmap.bitmapData;
				break;

			case 2: // cloned via clone()
				bitmapData = bitmap.bitmapData.clone();
				break;

			case 3: // cloned via copyPixels()
				bitmapData = new BitmapData(bitmap.bitmapData.width, bitmap.bitmapData.height, bitmap.bitmapData.transparent);
				bitmapData.copyPixels(bitmap.bitmapData, bitmap.bitmapData.rect, new Point() );
				break;

			case 4: // cloned via draw()
				bitmapData = new BitmapData(bitmap.bitmapData.width, bitmap.bitmapData.height, bitmap.bitmapData.transparent);
				bitmapData.draw(bitmap);
				break;
		}

		bitmap = new Bitmap(bitmapData);
		//this._bitmapData = bitmap.bitmapData;

		super.addChild(bitmap);
	}

	/**
	 * @private
	 */
	private function handler_progress(event:ProgressEvent):void {
		var li:LoaderInfo = event.target as LoaderInfo;
		trace('handler_progress, length', li.bytes.length);
		trace('handler_progress, bytesAvailable', li.bytes.bytesAvailable);
		//trace(li.bytes.readByte());
	}

	/**
	 * @private
	 */
	private function handler_httpStatus(event:HTTPStatusEvent):void {
		var li:LoaderInfo = event.target as LoaderInfo;
		trace('handler_httpStatus', event);
	}

}
}